<?php
session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../src/controllers/BookmarkController.php';
require_once __DIR__ . '/../src/controllers/UserController.php';

$bookmarkController = new BookmarkController($conn);
$userController = new UserController($conn);

$action = $_GET['action'] ?? 'home';

switch ($action) {
    case 'home':
        $bookmarks = $bookmarkController->getAll();
        $categories = $bookmarkController->getAllCategories();
        include __DIR__ . '/../src/views/home.php';
        break;
    case 'login':
        $userController->login();
        break;
    case 'logout':
        $userController->logout();
        break;
    case 'add_bookmark':
        if ($_SESSION['is_admin'] ?? false) {
            $bookmarkController->addBookmark();
        }
        header('Location: index.php');
        break;
    case 'admin':
        if ($_SESSION['is_admin'] ?? false) {
            $bookmarks = $bookmarkController->getAll();
            include __DIR__ . '/../src/views/admin.php';
        } else {
            header('Location: index.php');
        }
        break;
    case 'edit_bookmark':
        if ($_SESSION['is_admin'] ?? false) {
            $bookmarkController->editBookmark($_GET['id']);
        } else {
            header('Location: index.php');
        }
        break;
    case 'delete_bookmark':
        if ($_SESSION['is_admin'] ?? false) {
            $bookmarkController->deleteBookmark($_GET['id']);
        } else {
            header('Location: index.php');
        }
        break;
    default:
        header('HTTP/1.0 404 Not Found');
        echo '404 Not Found';
        break;
}